<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $images, $framework;
require_once("$framework/ajax.php");
/**
 * Html helpers to render easily some html text value.
 * @author frederic delorme
 *
 */
class Html{	

	
	/**
	 * Add a link with the standard $action for the entity $type passed on the $id.
	 * The text of the link is $label, a tooltip $description can be provided.
	 * if $image is!="" then, then $image is integrated to the link.
	 * $css_class can be used to specify one or more css classes.
	 * @param $action
	 * @param $type
	 * @param $id
	 * @param $label
	 * @param $$options{'accesskey'=>,'description'=>,'styleclass'=>}
	 * @return unknown_type
	 */
	public function link($manager="", $action="",$type="",$id="",$label,
					$options=array()){
						/*array('description'=>"",
									'styleclass'=>"",
									'image'=>"",
									'accesskey'=>"",
									'ajaxTarget' =>"",
									'ajaxOptions'=> array()*/
	
		$value="<a".(!empty($options['styleclass']) && $options['styleclass']!=""?" class=\"".$options['styleclass']."\" ":"").
			   	(!empty($options['ajaxTarget']) && $options['ajaxTarget']!=""
						? "" 
						: " href=\"".Html::encodeUrl($manager,$action,$type,$id)."\"").
		       (!empty($options['description']) && $options['description']!=""?" title=\"".$options['description']."\"":"").
		       (!empty($options['accesskey']) && $options['accesskey']!=""?" accesskey=\"".$options['accesskey']."\"":"").
		       (!empty($options['ajaxTarget']) && $options['ajaxTarget']!="" ? " onclick=\"".Ajax::remote($options['ajaxTarget'],Html::encodeUrl($manager,$action,$type,$id),(!empty($options['ajaxOptions'])?$options['ajaxOptions']:""))."\"":"").					
		       ">".
		       (!empty($options['image']) && $options['image']!=""?"<img src=\"".$options['image']."\" alt=\"".$options['image']."\">":"").
		       (!empty($options['accesskey'])?str_replace($options['accesskey'],"<u>".$options['accesskey']."</u>",$label):$label).
		       "</a>";
		return $value;
	}
	
	/**
	 * Convert string coming from db to the default encoding of the web site.
	 * following the encode type configured into system/encode from config.ini.
	 * @param $string
	 * @return encoded string.
	 */
	public function db($string){
		return mb_convert_encoding($string,__param("system","encode"),"iso-8859-1");
	}
	
	/**
	 * Translate a float number into a money value following the system/moneyformat key value
	 * into the config.ini file.
	 * @param $value
	 * @return money formated string.
	 */
	public function currency($value){
		return sprintf(__param("system","moneyformat"),$value);
	}
	/**
	 * Convert MysqlDate to PHP date to be displayed respecting the config.ini
	 * date format ("system","postdateformat").
	 * @param $mysqldate
	 * @return string containing formated date.
	 */
	public function dateFromDB($mysqldate){
		return date(__param("system","postdateformat"),strtotime($mysqldate));
	}
	/**
	 * encode Url for manager processing
	 * @param $manager
	 * @param $action
	 * @param $type
	 * @param $id
	 * @return unknown_type
	 */
	public function encodeUrl($manager="", $action="",$type="",$id=""){
		$rewrite=(__param("system","rewritemode")=="off"?"?/":"");
		return "$rewrite$manager/$action/$type/$id";
	}
	
	/**
	 * Create an image link integrating lightBox presentation.
	 * @param $pathImage
	 * @param $url
	 * @param $label
	 * @param $class
	 * @return string containing the image link.
	 */
	public function image($pathImage,$url="",$label="",$class="",$galery=""){
		$img="<img src=\"$pathImage\" title=\"$label\"". ($class!=""?" class=\"$class\"":"")."/>";
		$img=($url!=""?"<a href=\"$url\" rel=\"lightbox".($galery!=""?"[$galery]":"")."\" title=\"$label\">".$img."</a>":$img);
		return $img;
	}
}
?>
